import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/index.js'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		code: "",
		access_token: "",
		openid: "obD_t1CJQMV3LhLx0psum-iUdudI", // obD_t1DQGbgVrCFnluGvhS12uhR8
		appid: "",
		secret: "",
		masterColor: "#335FFC",
		isLogin: false,
		tabbar: [],
		goodsList: [],
		goodsItem:''
	},
	mutations: {
		setGoodsItem(state, rawData) {
			state.goodsItem = rawData;
		},
		setAccessToken(state, rawData) {
			state.access_token = rawData;
		},
		setOpenid(state, rawData) {
			state.openid = rawData;
		},
		setCode(state, rawData) {
			state.code = rawData;
		},
		setAppid(state, rawData) {
			state.appid = rawData;
		},
		setSecret(state, rawData) {
			state.secret = rawData;
		},
		setTabbar(state, rawData) {
			state.tabbar = rawData;
		},
		setColor(state, rawData) {
			state.mainColor = rawData;
			console.log(state.mainColor)
		},
		setMasterColor(state, rawData) {
			state.masterColor = rawData;
			console.log(state.masterColor)
		},
		SET_GOODS_LIST(state, rawData) {
			state.goodsList = rawData;
		},
	},
	actions: {
		SET_GOODS_LIST ({ commit }, data) {
		  commit('SET_GOODS_LIST',data);
		},
		//获取商品详情
		GET_GoodsItem ({ commit }, params) {
		  return new Promise((resolve, reject) => {
		    api.ProductItem(params).then(res => {
		      const result = res.data
		      commit('setGoodsItem', result)
		      resolve(result)
		    }).catch(error => {
		      reject(error)
		    })
		  })
		},
	},
	getters: {
		goodsItem:state=>state.goodsItem,
		goodsList:state=>state.goodsList,
		getOpenid(state) {
			return state.openid;
		},
		getAppid(state) {
			return state.appid;
		},
		getSecret(state) {
			return state.secret;
		},
		getMasterColor(state) {
			return state.masterColor;
		},
		getTabbar(state) {
			return state.tabbar;
		},
	},
	modules: {}
})
